// pages/chatsa/vipCategoryDetail/vipCategoryDetail.js
const app = getApp();
 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mess: '',
    data: '16:50',
    content: [
      {
        id: 1,
        text: '请问还在看机会吗?',
      },
      {
        id: 0,
        text: '您好,还在看',
      },
 
    ], //聊天信息
    mineAvatorSrc:
      'https://tse4-mm.cn.bing.net/th/id/OIP-C.PobTVuzuQy7tAZtvVEc3VQAAAA?rs=1&pid=ImgDetMain',
    himAvatorSrc:
      'https://tse4-mm.cn.bing.net/th/id/OIP-C.PobTVuzuQy7tAZtvVEc3VQAAAA?rs=1&pid=ImgDetMain',
    inputMessage: '',
    toBottom: '', // 用于控制滚动
    keepFocus: false, // 添加这个数据属性
    scrollTop: 0,
    keyboardHeight: 0,
    fontSizeScale: 1, // 添加字体缩放比例数据
    loading: false,
    pageNum: 1, // 添加页码控制,
    positionTop: 0,
    scrollViewHeight: 0,
    showEmojiPanel:false,
    emojiList: [
      '/pages/chat/img/0.jpg'
    ],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化字体大小
    this.setData({
      fontSizeScale: app.globalData.fontSizeScale,
    });
 
 
    setTimeout(() => {
      this.scrollToBottom();
    }, 50);
    // 监听键盘高度变化
    wx.onKeyboardHeightChange((res) => {
      const keyboardHeight = res.height;
      this.setData({
        keyboardHeight,
      });
    });
  },


  // 滑动到顶部
  handleScrollToupper() {
    if (this.data.loading) return; // 防止重复加载
 
    console.log('滑动到顶部');
    this.setData({
      loading: true,
    });
 
    // 记录当前第一条消息的位置
    const currentFirstMessage = this.data.content[0];
 
    setTimeout(() => {
      // 模拟获取历史消息
      const historyMessages = [
        {
          id: 1,
          text: '这是历史消息1',
        },
        {
          id: 0,
          text: '这是历史消息2',
        },
        {
          id: 1,
          text: '这是历史消息3',
        },
        {
          id: 0,
          text: '这是历史消息4',
        },
        // ... 更多历史消息
      ];

      this.setData(
        {
          content: [...historyMessages, ...this.data.content],
          loading: false,
          pageNum: this.data.pageNum + 1,
        },
      );
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // let imsJson = Array.from({ length: 2 }, (_, index) => index + 1);
    // let emojiList = []
    // imsJson.forEach(item=>{
    //   emojiList.push(`/pages/chat/img/${item}.jpg`)
    // })
    // this.setData({
    //   emojiList:emojiList
    // })

  },
 
  // 发送消息
  sendMess() {
    if (!this.data.inputMessage.trim()) return;
    const newMessage = {
      id: 1,
      text: this.data.inputMessage,
    };
    this.setData(
      {
        content: [...this.data.content, newMessage],
        inputMessage: '',
        keepFocus: true, // 保持输入框焦点
      },
      () => {
        this.scrollToBottom();
      }
    );
  },
 
  // 添加滚动到底部的方法
  scrollToBottom() {
    this.setData({
      scrollTop: 99999999,
    });
  },
  onFocus() {
    this.scrollToBottom();
  },
  // 输入事件处理
  onInput(e) {
    this.setData({
      inputMessage: e.detail.value,
    });
  },
 
  onUnload() {;
    // 页面卸载时移除监听
    wx.offKeyboardHeightChange();
  },


  toggleEmojiPanel() {
    this.setData({
      showEmojiPanel: !this.data.showEmojiPanel,
      keyboardHeight:this.data.showEmojiPanel==false?30:0
    });
    console.log(`showEmojiPanel`,this.data.showEmojiPanel,`keyboardHeight`,this.data.keyboardHeight)
  },


  selectEmoji(event) {
    const emojiPath = event.currentTarget.dataset.emoji;
    const newMessage = {
      id: 1,
      image: emojiPath,
      text:''
    };
    this.setData(
        {
          content: [...this.data.content, newMessage],
          inputMessage: '',
          keepFocus: true, // 保持输入框焦点
        },
        () => {
          this.scrollToBottom();
        }
    );
    console.log(`content`,this.data.content)
  },

});